<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../fast/dom/custom/testutils.js"></script>
<body>
<script>
'use strict';

// TODO(dominicc): Port fork() etc. to work with testharness, then
// remove these.
function debug() {}
function finishJSTest() {}

(() => {

if (fork()) {
  // The controlling parent frame.
  let t = async_test('constructor destroys the context retrieving prototype');
  let watcher = new EventWatcher(t, window, 'message');
  watcher.wait_for('message').then(t.step_func((event) => {
    assert_equals(event.data, 'PASS destroyed context');
    return watcher.wait_for('message');
  })).then(t.step_func((event) => {
    assert_equals(event.data, 'PASS child done');
    t.done();
  }));
} else {
  // The child frame.
  let should_destroy = false;
  let proxy;
  function BadConstructor() {
    should_destroy = true;
    return Reflect.construct(HTMLElement, [], proxy);
  }
  proxy = new Proxy(BadConstructor, {
    get(receiver, name) {
      if (should_destroy && name == 'prototype') {
        destroyContext();
        return 42;
      }
      return receiver[name];
    }
  });
  window.customElements.define('x-x', proxy);
  try {
    new proxy();
  } finally {
    done();
  }
}

})();
</script>
